package 牛客_面试必刷.Month07.day10;

import java.util.ArrayList;
import java.util.List;

public class 二叉树的前序遍历 {
    List<Integer> list = new ArrayList<>();
    public int[] preorderTraversal (TreeNode root) {
        // write code here
        preOrder(root);
        int[] arr = new int[list.size()];
        int index = 0;
        for(Integer i:list){
            arr[index++] = i;
        }
        return arr;
    }

    public void preOrder(TreeNode node){
        if(node == null) return;
        list.add(node.val);
        preOrder(node.left);
        preOrder(node.right);
    }
}
